#include "VWidget.h"
#include "VContainer.h"
#include "VMain.h"

VWidget::VWidget(void) :
    _x(0), _y(0), _w(0), _h(0),
    _mousetrace(false),
    _visible(false),
    _mousedown(false),
    _zorder(0),
    _privatedata(NULL)
{
    init();
}

VWidget::VWidget(int x, int y, int w, int h):
    _mousetrace(false),
    _visible(false),
    _mousedown(false),
    _zorder(0),
    _privatedata(NULL)
{
    setGeometry(x, y, w, h);
}

VWidget::~VWidget(void)
{
    SAFE_RELEASE(_privatedata);
}

void VWidget::setVisible(bool visible)
{
    this->_visible = visible;
}

bool VWidget::isVisible() const
{
    return this->_visible;
}

void VWidget::setGeometry(int x, int y, int w, int h)
{
    move(x, y);
    resize(w, h);
}

void VWidget::geometry(int* x, int* y, int* w, int* h) const
{
    pos(x, y);
    size(w, h);
}

void VWidget::move(int x, int y)
{
    this->_x = x;
    this->_y = y;
}


void VWidget::resize(int w, int h)
{
    this->_w = w;
    this->_h = h;
}

void VWidget::pos(int* x, int* y) const
{
    *x = this->_x;
    *y = this->_y;
}

void VWidget::size(int* w, int* h) const
{
    *w = this->_w;
    *h = this->_h;
}

int& VWidget::xpos()
{
    return this->_x;
}

int& VWidget::ypos()
{
    return this->_y;
}

int& VWidget::width()
{
    return this->_w;
}

int& VWidget::height()
{
    return this->_h;
}

bool VWidget::inArea(int x, int y) const
{
    UNUSED((x, y));
    return false;
}

bool VWidget::mouseDownEvent(int x, int y)
{
    if (inArea(x, y))
    {
        this->_mousedown = true;
        mouseDown(x, y);

        return true;
    }

    return false;
}

bool VWidget::mouseUpEvent(int x, int y)
{
    if (inArea(x, y))
    {
        if (this->_mousedown)
        {
            mouseClick(x, y);
            this->_mousedown = false;
        }

        mouseUp(x, y);
        return true;
    }

    return false;
}

bool VWidget::mouseMoveEvent(int x, int y, bool mousestat)
{
    if (!_mousetrace)
    {
        return false;
    }

    if (inArea(x, y))
    {
        mouseMove(x, y, mousestat);

        return true;
    }

    return false;
}

void VWidget::mouseDown(int x, int y)
{
    UNUSED((x, y));
    // Do Nothing
    return;
}

void VWidget::mouseUp(int x, int y)
{
    UNUSED((x, y));
    // Do Nothing
    return;
}

void VWidget::mouseClick(int x, int y)
{
    UNUSED((x, y));
    // Do Nothing
    return;
}

void VWidget::mouseMove(int x, int y, bool mousestat)
{
    UNUSED((x, y, mousestat));
    // Do Nothing
    return;
}

void VWidget::init()
{
    this->_visible = true;

    return;
}

CLASS_TYPE VWidget::classType() const
{
    return VWidget::_clstype;
}

/*void VWidget::setResponseArea( int x, int y, int w, int h )
{
    this->_resx = x;
    this->_resy = y;
    this->_resw = w;
    this->_resh = h;
}

void VWidget::responseArea( int* x, int* y, int* w, int* h ) const
{
*x = this->_resx;
*y = this->_resy;
*w = this->_resw;
*h = this->_resh;
}*/

bool VWidget::render() const
{
    if (!_visible)
    {
        return false;
    }

    return true;
}

void VWidget::animate(void)
{
    // donot do any animate
    return;
}

void VWidget::setMouseTrace(bool open)
{
    this->_mousetrace = open;
}

bool VWidget::mouseTrace() const
{
    return _mousetrace;
}

void VWidget::setZOrder(int zorder)
{
    _zorder = zorder;
}

int VWidget::zOrder() const
{
    return _zorder;
}

void* VWidget::privateData()
{
    return _privatedata;
}

void VWidget::setPrivateData(void* pdata)
{
    if (_privatedata)
    {
        delete _privatedata;
    }

    _privatedata = pdata;
}

void VWidget::setParent(VContainer* parent)
{
    this->_parent = parent;

    if (parent)
    {
        _parent->insert(this);
    }
}

VContainer* VWidget::parent(void)
{
    return _parent;
}